package com.canyou.captcha;

import com.canyou.constants.CommonConstants;
import com.canyou.utils.RedisUtils;
import com.canyou.utils.StringUtils;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

import static com.google.code.kaptcha.Constants.*;

/**
 * 验证码相关配置
 *
 * @author fy
 * @date 19/12/11 13:46
 */
@Configuration
public class KaptchaConfig {

    @Autowired
    private KaptchaConfigProperties kaptchaConfigProperties;

    /**
     * 默认生成图形验证码宽度
     */
    private static final String DEFAULT_IMAGE_WIDTH = "100";

    /**
     * 默认生成图像验证码高度
     */
    private static final String DEFAULT_IMAGE_HEIGHT = "40";

    /**
     * 默认生成图形验证码长度
     */
    private static final String DEFAULT_TEXTPRODUCER_CHAR_LENGTH = "4";

    /**
     * 边框颜色，合法值： r,g,b (and optional alpha) 或者 white,black,blue.
     */
    private static final String DEFAULT_FONT_COLOR = "black";

    /**
     * 图片边框
     */
    private static final String DEFAULT_BORDER = "no";

    /**
     * 默认边框颜色
     */
    private static final String DEFAULT_BORDER_COLOR = "black";

    /**
     * 默认图片间隔
     */
    private static final String DEFAULT_CHAR_SPACE = "5";

    /**
     * 验证码文字大小
     */
    private static final String DEFAULT_TEXTPRODUCER_FONT_SIZE = "30";

    /**
     * 默认保存code的前缀
     */
    private static final String DEFAULT_KAPTCHA_CODE_KEY = CommonConstants.CACHE_KAPTCHA_NAME_CODE;

    /**
     * 默认生成图形验证码过期时间(单位:s)
     */
    private static final int DEFAULT_IMAGE_EXPIRE = 90;

    @Bean
    public Producer producer() {
        Properties properties = new Properties();
        properties.put(KAPTCHA_BORDER, StringUtils.defaultString(kaptchaConfigProperties.getBorder(), DEFAULT_BORDER));
        properties.put(KAPTCHA_BORDER_COLOR, StringUtils.defaultString(kaptchaConfigProperties.getBorderColor(), DEFAULT_BORDER_COLOR));
        properties.put(KAPTCHA_TEXTPRODUCER_FONT_COLOR, StringUtils.defaultString(kaptchaConfigProperties.getFontColor(), DEFAULT_FONT_COLOR));
        properties.put(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, StringUtils.defaultString(kaptchaConfigProperties.getCharSpace(), DEFAULT_CHAR_SPACE));
        properties.put(KAPTCHA_IMAGE_WIDTH, StringUtils.defaultString(kaptchaConfigProperties.getImageWidth(), DEFAULT_IMAGE_WIDTH));
        properties.put(KAPTCHA_IMAGE_HEIGHT, StringUtils.defaultString(kaptchaConfigProperties.getImageHeight(), DEFAULT_IMAGE_HEIGHT));
        properties.put(KAPTCHA_TEXTPRODUCER_FONT_SIZE, StringUtils.defaultString(kaptchaConfigProperties.getFontSize(), DEFAULT_TEXTPRODUCER_FONT_SIZE));
        properties.put(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, StringUtils.defaultString(kaptchaConfigProperties.getCharLength(), DEFAULT_TEXTPRODUCER_CHAR_LENGTH));
        Config config = new Config(properties);
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }

    /**
     * 缓存验证码
     *
     * @param generateKey 缓存键
     * @param kaptchaCode 验证码值
     */
    public static void cacheKaptcha(String generateKey, String kaptchaCode) {
        RedisUtils.set(DEFAULT_KAPTCHA_CODE_KEY + generateKey, kaptchaCode, DEFAULT_IMAGE_EXPIRE);
    }

    /**
     * 从缓存读取验证码
     *
     * @param generateKey 缓存键
     * @return
     */
    public static String getKaptcha(String generateKey) {
        Object value = RedisUtils.get(DEFAULT_KAPTCHA_CODE_KEY + generateKey);
        if (value instanceof String) {
            return String.valueOf(value);
        }
        return null;
    }

}
